package cn.yunlang.zhangZhongFei.vo.response;

import cn.yunlang.zhangZhongFei.exception.ErrorCodeExceptionEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

@Data
@Log4j2
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class R<T> {
    @ApiModelProperty(value = "响应码", required = true)
    private Integer code;

    @ApiModelProperty(value = "响应信息", required = true)
    private String msg;

    @ApiModelProperty(value = "响应数据")
    private T data;

    private static final Integer OK_CODE = 200;

    private static final String OK_MSG = "OK";

    /**
     * 三个参数的构造函数（用于返回有数据的接口）
     */
    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 两个参数的构造函数（用于返回没有数据，但要返回信息的接口）
     */
    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 无参调用两个参数的构造函数（不用每次都编写返回码和返回信息，直接调用该方法）
     */
    public R() {
        this(OK_CODE, OK_MSG);
    }

    /**
     * 返回一个通用的成功方法
     */
    public static <T> R<T> success() {
        return new R<>();
    }

    /**
     * 返回一个通用的成功方法并且有返回值的
     */
    public static <T> R<T> success(T result) {
        R<T> response = new R<>();
        response.setData(result);
        return response;
    }
    public static <T> R<T> error(Integer code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> error(ErrorCodeExceptionEnum errorCodeExceptionenum) {
        return new R<>(errorCodeExceptionenum.getCode(), errorCodeExceptionenum.getMsg());
    }




}
